let util = module.exports = {
    isNumber: function (obj) {
        return !isNaN(parseFloat(obj)) && isFinite(obj);
    },
    isString: function (obj) {
        return Object.prototype.toString.call(obj).indexOf('String]') > -1;
    },
    isFunction: function (obj) {
        return Object.prototype.toString.call(obj).indexOf('Function]') > -1;
    },
    isFunc: function (obj) {
        return this.isFunction(obj);
    },
    isPromise: function (obj) {
        return Object.prototype.toString.call(obj).indexOf('Promise]') > -1;
    },
    isArray: function (obj) {
        return Object.prototype.toString.call(obj).indexOf('Array]') > -1;
    },
    isObject: function (obj) {
        return Object.prototype.toString.call(obj).indexOf('Object]') > -1;
    },
    isRegExp: function (obj) {
        return Object.prototype.toString.call(obj).indexOf('RegExp]') > -1;
    },
    isElement: function (obj) {
        return obj && obj.nodeType === 1;
    },
    index: function (list, value, field) {
        if (util.isArray(list)) {
            for (let i = 0; i < list.length; i++) {
                let ret = field == null ? list[i] : list[i][field];
                if (ret == value) {
                    return i;
                }
            }
        } else if (util.isObject(list)) {
            for (let i in list) {
                if (Object.prototype.hasOwnProperty.call(list, i)) {
                    let ret = field == null ? list[i] : list[i][field];
                    if (ret == value) {
                        return i;
                    }
                }
            }
        }
        return null;
    },
    find: function (list, value, field) {
        var index = util.index.apply(util, arguments);
        if (index !== null) {
            return list[index];
        } else {
            return null;
        }
    },
    exist: function(list, value, field) {
        return util.index.apply(util, arguments) != null;
    },
    remove: function(list, value, field) {
        var index = util.index.apply(util, arguments);
        if (index == null) {
            return false;
        }

        if (util.isArray(list)) {
            list.splice(index, 1);
        } else if (util.isObject(list)) {
            delete list[index];
        } else {
            return false;
        }

        return true;
    },
    /**
     * 数字或字符串前面补0
     *
     * @param {(number|string)} num
     * @param {number} length
     * @returns
     */
    fix0: function(num, length) {
        num = String(num);
        while (num.length < length) {
            num = '0' + num;
        }
        return num;
    },
    toDate: function(timestamp) {
        var date;
        if (this.isNumber(timestamp) && String(timestamp).length == 10) {
            timestamp = timestamp * 1e3;
        }

        if (this.isNumber(timestamp)) {
            date = new Date();
            date.setTime(timestamp);
        } else if (isDate(timestamp)) {
            date = new Date(timestamp.getTime());
        } else {
            date = new Date();
        }

        return date;
    },

    /**
     * 两个日期之间的天数差异
     *
     * @param {(number|Date)} timestamp1
     * @param {(number|Date)} timestamp2
     */
    dateDiff: function(timestamp1, timestamp2) {
        var date1 = this.toDate(timestamp1);
        var date2 = this.toDate(timestamp2);

        date1.setHours(0, 0, 0, 0);
        date2.setHours(0, 0, 0, 0);

        return (date1.getTime() - date2.getTime()) / 86400;
    },
    dayList: ["日", "一", "二", "三", "四", "五", "六"],
    getWeek: function(timestamp) {
        var date = this.toDate(timestamp);
        var day  = new Date().getDay();

        return "周" + dayList[day];
    },
    format_callback: function(t, tag) {
        switch (tag){
            case 'Y': return t.getFullYear();
            case 'y': return t.getFullYear() % 100;
            case 'm': return this.fix0(t.getMonth()+1, 2);
            case 'n': return t.getMonth()+1;
            case 'd': return this.fix0(t.getDate(), 2);
            case 'j': return t.getDate();
            case 'N': return t.getDay();
            case 'w': return t.getDay() % 7;
            case 'a': return t.getHours() < 12 ? 'am':'pm';
            case 'A': return t.getHours() < 12 ? 'AM':'PM';
            case 'g': return t.getHours() % 12;
            case 'h': return this.fix0(t.getHours() % 12, 2);
            case 'G': return t.getHours();
            case 'H': return this.fix0(t.getHours(), 2);
            case 'i': return this.fix0(t.getMinutes(), 2);
            case 's': return this.fix0(t.getSeconds(), 2);
        }
        return tag;
    },
    date_format_reg: /[YymndjNwaAghGHis]/g,
    /**
     * 日期格式化
     *
     * @param {(number|Date)} timestamp
     * @param {string} format
     * @returns
     */
    date: function(timestamp, format) {
        var date = this.toDate(timestamp);
        return format.replace(this.date_format_reg, this.format_callback.bind(this, date));
    },
    // 转换时间为字符串
    getDateDiff: function(timeStamp) {
        var minute = 1000 * 60;
        var hour = minute * 60;
        var day = hour * 24;
        var halfamonth = day * 15;
        var month = day * 30;
        var now = new Date().getTime();
        var diffValue = now - timeStamp*1000;
        // if(diffValue < 0){return;}
        var monthC = diffValue/month;
        var weekC = diffValue/(7*day);
        var dayC = diffValue/day;
        var hourC = diffValue/hour;
        var minC = diffValue/minute;
        if(monthC >= 1){
			result = "" + parseInt(monthC) + "月前";
			if(monthC > 12){
				result = this.date(timeStamp, "Y-m-d H:i");
			}
        }
        else if(weekC >= 1){
			result = "" + parseInt(weekC) + "周前";
        }
        else if(dayC >= 1){
			result = ""+ parseInt(dayC) +"天前";
        }
        else if(hourC >= 1){
			result = ""+ parseInt(hourC) +"小时前";
        }
        else if(minC >= 1){
			result = ""+ parseInt(minC) +"分钟前";
        }else {
			result = "刚刚";
        }

        return result;
    }
}


